home *** CD-ROM | disk | FTP | other *** search
/ Deutsche Edition 1 / Deutsche Edition 1.iso / amok / amok_lha / amok15.lha / Seafarers_Manual / Source / C4P4.mod < prev    next >
Text File  |  1993-08-15  |  2KB  |  84 lines

  1. MODULE C4P4;  (* Chapter 4  Problem 4 *)
  2.           (* Added a check if problem can be evaluated *)
  3.               (* Program RaisePower4 to accept negative integer value also *)
  4.               (* RaisePower4: Raise x to y power using function procedure *)
  5.  
  6.   (* From the book "Modula-2  A Seafarer's Manual and Shipyard Guide" *)
  7.   (* Page 121   adapted "Amiga M2Modula-2"   08 Mar 1988 *)
  8.  
  9. FROM InOut IMPORT WriteLn,
  10.                   WriteString,
  11.                   ReadInt;
  12. FROM RealInOut IMPORT WriteReal,
  13.                       ReadReal;
  14.                       
  15. VAR
  16.   z,                    (* receives result *)
  17.   x : REAL;                (* base from keyboard *)
  18.   y : INTEGER;                (* exponent from keyboard *)
  19.   CanDo : BOOLEAN;            (* TRUE if power can be evaluated *)
  20.     
  21. PROCEDURE Getxy(VAR base : REAL;    (* get x & y from keyboard *)
  22.                 VAR exp : INTEGER);
  23.   BEGIN
  24.     WriteLn;
  25.     WriteString ("Enter x: ");
  26.     ReadReal (base);
  27.     WriteLn;
  28.     WriteString ("Enter y: ");
  29.     ReadInt (exp);
  30.   END Getxy;
  31.   
  32. PROCEDURE Power(t : REAL;        (* base *)
  33.                 e : INTEGER;        (* exponent *)
  34.                 VAR OK : BOOLEAN) : REAL;    (* power can be evaluated? *)
  35.   VAR
  36.     xy : REAL;
  37.     Positive : BOOLEAN;
  38.   BEGIN
  39.     OK := ((t # 0.0) OR (e # 0));
  40.     IF NOT OK THEN            (* power cannot be evaluated *)
  41.       RETURN 0.0;
  42.     END;
  43.     Positive := (e >= 0);        (* Positive is TRUE when e >= 0 *)
  44.     e := ABS (e);
  45.     xy := 1.0;                (* initialize result *)
  46.     WHILE (e # 0) DO
  47.       WHILE (NOT ODD(e)) DO
  48.         t := t * t;
  49.         e := e DIV 2;
  50.       END;   (* WHILE NOT *)
  51.       xy := xy * t;
  52.       DEC (e);
  53.     END;   (* WHILE e *)
  54.     IF NOT Positive THEN        (* calculate result for e < 0 *)
  55.       xy := 1.0 / xy;
  56.     END;
  57.     RETURN xy;                (* return result *)
  58.   END Power;
  59.    
  60. PROCEDURE DisplayAnswer(xtoy : REAL);
  61.   BEGIN
  62.     WriteLn;
  63.     WriteString ("x to y power = ");
  64.     WriteReal (xtoy,10,2);
  65.     WriteLn;
  66.   END DisplayAnswer;
  67.  
  68. BEGIN                    (* MODULE RaisePower *)
  69.   WriteLn;
  70.   WriteString ("Calculating x to y power");
  71.   Getxy(x,y);
  72.   z := Power(x,y,CanDo);
  73.   IF CanDo THEN
  74.     DisplayAnswer(z);
  75.   ELSE
  76.     WriteLn;
  77.     WriteString ("x to y power cannot be evaluated.");
  78.     WriteLn;
  79.     WriteString ("x and y are equal to 0 !!!   This is undefined.");
  80.     WriteLn;
  81.   END;
  82.   
  83. END C4P4.
  84.